package com.bins.dentist.clinic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bins.dentist.clinic.constants.CommonConstants;
import com.bins.dentist.clinic.mapper.RoleMapper;
import com.bins.dentist.clinic.model.dto.RoleDto;
import com.bins.dentist.clinic.model.dto.RolePageDto;
import com.bins.dentist.clinic.model.dto.RolePermDto;
import com.bins.dentist.clinic.entity.PermissionEntity;
import com.bins.dentist.clinic.entity.RoleEntity;
import com.bins.dentist.clinic.entity.RolePermissionEntity;
import com.bins.dentist.clinic.model.vo.RolePermInfoVo;
import com.bins.dentist.clinic.model.vo.RolePermVo;
import com.bins.dentist.clinic.model.vo.RoleVo;
import com.bins.dentist.clinic.model.vo.SimpleSelectVo;
import com.bins.dentist.clinic.service.PermissionService;
import com.bins.dentist.clinic.service.RolePermissionService;
import com.bins.dentist.clinic.service.RoleService;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bins
 * @since 2023-02-20 20:53:31
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Override
    public IPage<RoleVo> getPage(RolePageDto pageDto) {
        LambdaQueryWrapper<RoleEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(!ObjectUtils.isEmpty(pageDto.getRoleCode()), RoleEntity::getRoleCode, pageDto.getRoleCode())
                .like(!ObjectUtils.isEmpty(pageDto.getRoleName()), RoleEntity::getRoleName, pageDto.getRoleName())
                .eq(RoleEntity::getIsDel, CommonConstants.DELETED_NO);
        IPage page = roleMapper.selectPage(new Page(pageDto.getPageNum(), pageDto.getPageSize()), queryWrapper);
        List<RoleEntity> rolePageList = page.getRecords();
        if (CollectionUtils.isEmpty(rolePageList)) {
            return page;
        }
        List<RoleVo> roleList = Lists.newArrayListWithCapacity(rolePageList.size());
        for (RoleEntity userEntity : rolePageList) {
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(userEntity, roleVo);
            roleList.add(roleVo);
        }
        page.setRecords(roleList);
        return page;
    }

    @Override
    public RoleVo getDetailById(RoleDto dto) {
        RoleEntity roleEntity = roleMapper.selectById(dto.getId());
        RoleVo roleVo = new RoleVo();
        BeanUtils.copyProperties(roleEntity, roleVo);
        return roleVo;
    }

    @Override
    public RoleVo saveRole(RoleDto dto) {
        RoleEntity roleEntity = new RoleEntity();
        if (ObjectUtils.isEmpty(dto.getId())) {
            BeanUtils.copyProperties(dto, roleEntity);
            roleEntity.setIsDel(CommonConstants.DELETED_NO);
            roleEntity.setCreateBy(0l);
            roleEntity.setUpdateBy(0l);
            roleEntity.setCreateTime(LocalDateTime.now());
            roleEntity.setUpdateTime(LocalDateTime.now());
            roleMapper.insert(roleEntity);
        } else {
            roleEntity.setId(dto.getId());
            roleEntity.setSort(dto.getSort());
            roleEntity.setRoleCode(dto.getRoleCode());
            roleEntity.setRoleName(dto.getRoleName());
            roleMapper.updateById(roleEntity);
        }
        RoleVo roleVo = new RoleVo();
        roleVo.setId(roleEntity.getId());
        roleVo.setRoleName(roleEntity.getRoleName());
        roleVo.setRoleCode(roleEntity.getRoleCode());
        return roleVo;
    }

    @Override
    public Integer deleteRole(RoleDto dto) {
        RoleEntity roleEntity = roleMapper.selectById(dto.getId());
        RoleEntity entity = new RoleEntity();
        entity.setId(dto.getId());
        entity.setIsDel(CommonConstants.DELETED_YES);
        return roleMapper.updateById(entity);
    }

    @Override
    public List<SimpleSelectVo> roleSelect(RoleDto dto) {
        LambdaQueryWrapper<RoleEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(RoleEntity::getIsDel, CommonConstants.DELETED_NO);
        List<RoleEntity> roleList = roleMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(roleList)) {
            return Collections.emptyList();
        }
        List<SimpleSelectVo> resultList = Lists.newArrayListWithCapacity(roleList.size());
        SimpleSelectVo simpleSelectVo;
        for (RoleEntity roleEntity : roleList) {
            simpleSelectVo = new SimpleSelectVo();
            simpleSelectVo.setId(roleEntity.getId());
            simpleSelectVo.setName(roleEntity.getRoleName());
            resultList.add(simpleSelectVo);
        }
        return resultList;
    }

    @Override
    public List<RolePermVo> rolePermList(RoleDto dto) {
        List<PermissionEntity> permList = permissionService.findAll();
        if (CollectionUtils.isEmpty(permList)) {
            return Collections.emptyList();
        }
        List<RolePermVo> rolePermList = Lists.newArrayListWithCapacity(permList.size());
        RolePermVo rolePermVo;
        for (PermissionEntity perm : permList) {
            rolePermVo = new RolePermVo();
            rolePermVo.setId(perm.getId());
            rolePermVo.setLabel(perm.getPermissionName());
            rolePermVo.setChecked(Boolean.FALSE);
            rolePermVo.setPid(perm.getPid());
            rolePermList.add(rolePermVo);
        }
        return rolePermList;
    }

    @Override
    public RolePermInfoVo rolePermInfo(RoleDto dto) {
        RolePermInfoVo resultVo = new RolePermInfoVo();
        resultVo.setPermTree(permissionService.permTreeList());
        resultVo.setCheckedIds(rolePermissionService.getIdsByRoleId(dto.getId()));
        return resultVo;
    }

    @Override
    public RoleVo saveRolePerm(RolePermDto dto) {
        RoleEntity roleEntity = roleMapper.selectById(dto.getRoleId());
        rolePermissionService.deleteByRoleId(dto.getRoleId());
        List<RolePermissionEntity> perms = rolePermissionService.buildRolePermList(dto);
        rolePermissionService.saveBatch(perms);
        RoleVo resultVo = new RoleVo();
        resultVo.setId(roleEntity.getId());
        return resultVo;
    }
}
